package com.rose.sharding.data.controller;

import com.rose.sharding.data.entity.User;
import com.rose.sharding.data.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author: zhu.chen
 * @date: 2020/10/29
 * @version: v1.0.0
 */
@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    /**
     * http://localhost:8084/sharding/insert
     */
    @GetMapping("/insert")
    public String insert() {
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setName("test" + i);
            user.setCityId(i % 2 == 0 ? 1 : 2);
            user.setCreateTime(new Date());
            user.setSex(i % 2 == 0 ? 1 : 2);
            user.setPhone("11111111" + i);
            user.setEmail("xxxxx");
            user.setCreateTime(new Date());
            user.setPassword("eeeeeeeeeeee");
            userMapper.insert(user);
        }
        return "ok";
    }

    /**
     * http://localhost:8084/sharding/select?username=xiaohong
     */
    @GetMapping("/select")
    public User select(@RequestParam("name") String name) {
        return userMapper.select(name);
    }


}
